(function ($) {
    $.fn.robotCsRender = function (options) {
        var containerParent = $(this);
        var _options = {
            skinCode: "default",
            iframeUrl: "/index.html",
            tip: "丁小姐，早上好开启您的梦想之旅",
            desc: "我是一个小小的探险家，要不要跟我一起来探险？"
        }
        $.extend(_options, options)
        var themeCss = $("head link.skin-theme[title='" + _options.skinCode + "']");
        if (themeCss.size() <= 0) {
            themeCss = $("head link.skin-theme[title='default']");
        }
        themeCss.removeAttr("disabled").siblings().prop("disabled", "disabled")


        var tip = _options.tip || ""
        var desc = _options.desc || ""
        var robotCsHtml =
            '<!--机器人内容-->' +
            '<div class="content-box">' +
            '  <div class="robot">' +
            '    <div class="robot-box">' +
            '        <div class="head">' +
            // '            <img src="static/image/head.png"/>' +
            '            <div class="ripple">' +
            '                <span><i></i></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '                <span></span>' +
            '            </div>' +
            '            <!--线性波-->' +
            '            <div class="waveWrapper waveAnimation">' +
            '                <div class="waveWrapperInner bgMiddle">' +
            '                    <div class="wave waveMiddle" style="background-image: url(\'./static/image/line2.png\')"></div>' +
            '                </div>' +
            '            </div>' +
            '        </div>' +
            '        <div class="pregnancy">' +
            '          <div class="pregnancy-clothes"></div>' +
            '        </div>' +
            '        <!--<div class="arm"><img src="static/image/arm.png"/></div>-->' +
            '    </div>' +
            '    <div class="robot-notice-box">' +
            '        <h1>' + tip + '</h1>' +
            '        <p>' + desc + '</p>' +
            '    </div>' +
            '    </div>' +
            '  </div>' +
            '</div>';
        //
        var robotContainer = $(robotCsHtml);
        containerParent.append(robotContainer);

        // 闭包
        (function () {
            var DEFAULT_VERSION = 8.0;
            var ua = navigator.userAgent.toLowerCase();
            var isIE = ua.indexOf("msie") > -1;
            var iEVersion;
            if (isIE) {
                iEVersion = ua.match(/msie ([\d.]+)/)[1];
            }
            if (iEVersion <= DEFAULT_VERSION) {
                hideRobot()
                return;
            }

            /**
             * 获取 robot 容器
             * */
            function getContainer() {
                return $(".robot");
            }

            /**
             * 获取 robot 容器
             * */
            function hideRobot() {
                getContainer().hide();
            }

            /**
             *  展示robot-text 容器
             * */
            function showRobotText() {
                getContainer().find(".robot-notice-box").show();
            }

            /**
             *  隐藏robot-text 容器
             * */
            function hideRobotText() {
                getContainer().find(".robot-notice-box").hide();
            }

            // // 1. 拖拽
            // (function () {
            //     var elem = getContainer()[0];
            //     var dragger = new Draggabilly(elem, {
            //         containment: containerParent[0]
            //     });
            // })();


            // 2. 双击，未来展示对话框
            (function () {
                var robotMan = getContainer().find(".robot-box");
                robotMan.dblclick(function () {
                    var iframe_chat_window = $("#iframe_window_chat");
                    if (iframe_chat_window.size() <= 0) {
                        var iframe = $("<iframe></iframe>");
                        iframe.attr("id", "iframe_window_chat");
                        iframe.attr("name", "iframe_window_chat");
                        iframe.attr("frameborder", "0");
                        iframe.attr("scrolling", "no");
                        iframe.attr("width", "100%");
                        iframe.attr("src", _options.iframeUrl);
                        iframe.addClass("iframe_window_chat");
                        iframe.on("load", function (event) {
                            var chatWindow = $(this);

                            $(this).prop("height", $(this).contents().height() + "px")
                            $(this).css("height", "100%")
                            $(this).contents()
                                .find(".chat_window_close")
                                .click(function () {
                                    chatWindow.remove();
                                });
                        });
                        $("body").append(iframe)
                    }
                })
            })();


            // 3.超时显示 机器人提示语
            (function () {
                // 鼠标移入
                var robot = getContainer().find(".robot-box");
                robot.mouseenter(function () {
                    showRobotText();
                })
            })();
        })();
    }
})(jQuery)